package com.atguigu.spring.aop.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 林允
 * @version 1.0
 * @description: TODO
 * @date 2023/5/9 21:05
 */
@Component
@Aspect
public class DemoAspect {

    @Pointcut("execution(* com.atguigu.spring.aop.annotation.DemoImpl.*(..))")
    public void pointCutDemo(){

    }

    @Before("pointCutDemo()")
    public void beforeTest(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        System.out.println("前置通知测试" + signature.getName());
    }

    @After("pointCutDemo()")
    public void afterTest(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        System.out.println("后置通知测试" + signature.getName());
    }

    @AfterReturning(value = "pointCutDemo()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result){
        Signature signature = joinPoint.getSignature();
        System.out.println("返回通知测试" + signature.getName() + "结果:" + result);
    }

    @AfterThrowing(value = "pointCutDemo()", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Exception ex){
        Signature signature = joinPoint.getSignature();
        System.out.println("异常通知测试" + signature.getName() + "异常:" + ex);
    }

    @Around("pointCutDemo()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result = null;
        try {
            Signature signature = joinPoint.getSignature();
            System.out.println("环绕通知--> 前置" + signature.getName());
            result = joinPoint.proceed();
            System.out.println("环绕通知--> 返回" + signature.getName() + result);
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("环绕通知--> 异常");
        } finally {
            System.out.println("环绕通知--> 后置");
        }
        return result;
    }

}
